package org.csu.webjpetstoreback.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.csu.webjpetstoreback.config.CommonResponse;
import org.csu.webjpetstoreback.model.Orders;
import org.csu.webjpetstoreback.model.Signon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.http.HttpRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ControllerDemo {

    @Autowired
    Orders order;//如果用自动注入的方式返回，会有很多其他的键值对，还不知道怎么去掉这些数据。

    @RequestMapping({"/testUrl"})
    @ResponseBody
    public String testUrl(){
        return "Hello World!";
    }

    /**
     * 测试后端向前端传输数据
     * @return
     */
    @GetMapping("/orders/ordersSessionDemo/")
    @ResponseBody
    @CrossOrigin(originPatterns = "*", allowCredentials = "true")
    public CommonResponse<List<Orders>> getorderSession(){
        List<Orders> orderList = new ArrayList<>();
        //如果用自动注入的方式返回，会有很多其他的键值对，还不知道怎么去掉这些数据。所以这边还是用新建订单对象的方式。
        Orders order1 = new Orders();
        order1.setOrderId(8888);
        order1.setUsername("ch");
        order1.setShipAddress1("china");
        order1.setShipAddress2("hunan");
        order1.setShipCity("changsha");
        order1.setBillToLastName("Chen");
        order1.setBillAddress2("Long");
        orderList.add(order1);
        return CommonResponse.BackForSuccess(orderList);
    }


//    /**
//     * 测试前端向后端传输数据
//     * @param orders
//     * @param httpSession
//     * @return
//     */
//    @PostMapping("/orders/setOrdersSessionDemo")
//    @ResponseBody
//    @CrossOrigin(originPatterns = "*", allowCredentials = "true")
//    public CommonResponse<String> setorderSession(
//            @RequestBody Orders orders,
//            HttpSession httpSession
//    ){
//        System.out.println(orders);
//        httpSession.setAttribute("name",orders.getUsername());
//        System.out.println(httpSession.getAttribute("name"));
//        return CommonResponse.BackForSuccess("前端向后端传输数据成功！orderSession已被设置");
//    }

    @PostMapping("/loginDemo")
    @ResponseBody
    @CrossOrigin(originPatterns = "*", allowCredentials = "true")
    public CommonResponse<String> Login(
            @RequestBody Signon signon,
            HttpSession httpSession
    ){
        System.out.println(signon);
        return CommonResponse.BackForSuccess("前端向后端传输数据成功！signon已被设置");
    }

    @GetMapping("/loginDemo2")
    @ResponseBody
    @CrossOrigin(originPatterns = "*", allowCredentials = "true")
    public CommonResponse<String> Login2(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpSession httpSession
    ){
        System.out.println(username);
        return CommonResponse.BackForSuccess("前端向后端传输数据成功！signon已被设置");
    }

    @GetMapping("/orders/testSession")
    @ResponseBody
    public CommonResponse<String> testSession(
            HttpSession session
    ){
        String sessionId= session.getId();
        return CommonResponse.BackForSuccess(sessionId);
    }

    @ResponseBody
    @PostMapping("/testRequest/{id}")
    String testRequest(
            @PathVariable("id") Integer id,
            @RequestParam("a") String a,
            @RequestParam("b") String b
    ){
        System.out.println(a+b);
        return "abc";
    }



//这种适用于参数不是ResponseBody，更具有普遍性。
    @PostMapping("/orders/setOrdersSessionDemo")
    @ResponseBody
    @CrossOrigin(originPatterns = "*", allowCredentials = "true")
    public String setorderSession(
            HttpServletRequest request
    ){
        String requestBody="";
        try {
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            requestBody = sb.toString();
            /**
             * requestBody是形如
             * {"orderId":"","username":"","orderDate":"","shipAddress1":"","shipAddress2":"","shipCity":"","shipState":"","shipZip":"","shipCountry":"","billAddress1":"","billAddress2":"","billCity":"","billZip":"","billCountry":"","billstate":"","courier":"","totalPrice":"","billToFirstName":"","billToLastName":"","shipToFirstName":"","shipToLastName":"","creditCard":"","expiryDate":"","cardType":"","locale":"","status":"","state":"","sendOut":""}
             * [Orders(orderId=8888, username=ch, orderDate=null, shipAddress1=china, shipAddress2=hunan, shipCity=changsha, shipState=null, shipZip=null, shipCountry=null, billAddress1=null, billAddress2=Long, billCity=null, billZip=null, billCountry=null, billstate=null, courier=null, totalPrice=null, billToFirstName=null, billToLastName=Chen, shipToFirstName=null, shipToLastName=null, creditCard=null, expiryDate=null, cardType=null, locale=null, status=null, state=null, sendOut=null, lineItems=[])]
             * 的数据
             */
            System.out.println(requestBody);
            return requestBody;
            // 处理requestBody数据
        } catch (IOException e) {
            // 处理异常
        }
        return requestBody;
    }

}
